import Cookies from 'js-cookie'
const TokenKey = 'token'
const UserKey = 'userInfo'
const orderNoKey = 'orderNo'
const localDownloadConfigKey = 'createNew/localDownloadConfig'


// token相关
export function getToken() {
  const token = Cookies.get(TokenKey)
  return token || ''
}

export function setToken(token) {
  return Cookies.set(TokenKey, token, { expires: 30 })
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}


export function getUserInfo() {
  const userInfo = Cookies.get(UserKey)
  if (userInfo && userInfo !== 'undefined') {
    return JSON.parse(userInfo)
  } else {
    return null
  }
}

export function setUserInfo(userInfo) {
  const userStr = JSON.stringify(userInfo)
  return Cookies.set(UserKey, userStr, { expires: 30 })
}

export function removeUserInfo() {
  return Cookies.remove(UserKey)
}

// 设置订单号
export function getOrderNo() {
  const orderno = Cookies.get(orderNoKey) || ''
  return orderno
}


export function setOrderNo(orderNo) {
  return Cookies.set(orderNoKey, orderNo, { expires: 7 })
}

export function removeOrderNo() {
  return Cookies.remove(orderNoKey)
}

// 下载相关设置
export function getLocalDownloadConfig() {
  const localDownloadConfig = window.localStorage.getItem(localDownloadConfigKey)
  if (localDownloadConfig) {
    let jParseData = JSON.parse(localDownloadConfig)
    return jParseData
  } else {
    return null
  }

}

export function setLocalDownloadConfig(data) {
  let dataStr = JSON.stringify(data)
  window.localStorage.setItem(localDownloadConfigKey, dataStr, 30, 'd')
}

// 是否显示通知

export function getNoticeReadStatus(key) {
  return window.localStorage.getItem('notice/' + key)
}

export function setNoticeReadStatus(key, value) {
  window.localStorage.setItem('notice/' + key, value)
}


export function getDownloadData(key) {
  let data = window.localStorage.getItem(key + '/downloadData')
  if (data && data !== 'null' && data !== 'undefined') {
    let list = JSON.parse(data)
    if (Array.isArray(list)) {
      return list
    } else {
      return null
    }

  } else {
    return null
  }

}

export function setDownloadData(key, value) {

  window.localStorage.setItem(key + '/downloadData', JSON.stringify(value))
}

export function clearDownloadData(key) {
  window.localStorage.setItem(key + '/downloadData', '')
}

